package com.atsmart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.TemplateInfo;
import com.atsmart.app.model.TemplateListInfo;

public class ReportListAdapter extends LoadingAdapter {
	private TemplateListInfo datasource;
	private LayoutInflater inflater;

	public ReportListAdapter(TemplateListInfo datasource){
		this.datasource = datasource;
		inflater = (LayoutInflater) BizApp.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void appendDataSource(TemplateListInfo appendData){
		datasource.getTemplates().addAll(appendData.getTemplates());
	}

	private static class ViewHolder{
		TextView title;
		TextView desc;
		ImageView img;
	}

	@Override
	public int getCount() {
		if (datasource.getTemplates() == null || datasource.getTemplates().size() == 0) {
            return 0;
        }

        if (delegate != null && delegate.needLoading()) {
            return datasource.getTemplates().size() + 1;
        } else {
            return datasource.getTemplates().size();
        }
	}

	@Override
	public Object getItem(int position) {
		 if (datasource.getTemplates() == null || position == datasource.getTemplates().size()
	                && delegate != null && delegate.needLoading()) {
	            return null;
	        }
		return datasource.getTemplates().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
        if (loadingRow != null) {
            return loadingRow;
        }
        return getView(position, convertView);
	}

	private View getView(int position, View convertView){
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			convertView = inflater.inflate(R.layout.report_list_item_layout, null);
			vh = new ViewHolder();
			vh.title = (TextView) convertView.findViewById(R.id.title);
			vh.desc = (TextView) convertView.findViewById(R.id.desc);
			vh.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		TemplateInfo templateInfo = datasource.getTemplates().get(position);
		if(templateInfo.getUpdate_type()==1){
			
		}
		if(position%2==1){
			convertView.setBackgroundResource(R.color.bottom_gray);
		}else{
			convertView.setBackgroundResource(R.color.white);
		}
		vh.title.setText(templateInfo.getName());
		vh.desc.setText(templateInfo.getDesc());
		if(templateInfo.getUpdate_type()==1){
			vh.img.setBackgroundResource(R.drawable.checked_true);
		}
		return convertView;
	}

	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getTemplates().size();
	}


}
